import data_loader
import csv
import mapping
import pandas as pd


def convert_xlsx_to_csv(xlsx_file, csv_file):
    # 读取Excel文件
    df = pd.read_excel(xlsx_file)

    # 将数据写入CSV文件
    df.to_csv(csv_file, index=False)


def generate_1xcsv():
    data = data_loader.load_data1()

    with open(
        "c:/Users/16028/Desktop/mcm/2023c/proj/data/1x.csv",
        mode="w",
        newline="",
        encoding="utf-8",
    ) as file:
        writer = csv.writer(file)
        writer.writerow(("code", "type"))
        for i in range(len(data)):
            writer.writerow((i + 1, mapping.get_type(i + 1)))


def generate_2xcsv():
    data = data_loader.load_data2()
    with open(
        "c:/Users/16028/Desktop/mcm/2023c/proj/data/2x.csv",
        mode="w",
        newline="",
        encoding="utf-8",
    ) as file:
        writer = csv.writer(file)
        writer.writerow(("date", "code", "quantity", "price", "discounted"))
        for i in range(len(data)):
            x = data[i]
            is_discounted = 0
            if x["is_discounted"] == "是":
                is_discounted = 1
            writer.writerow(
                (
                    x["sale_date"],
                    mapping.code_to_code(x["product_code"]),
                    x["quantity"],
                    x["unit_price"],
                    is_discounted,
                )
            )


def generate_3xcsv():
    data = data_loader.load_data3()
    with open(
        "c:/Users/16028/Desktop/mcm/2023c/proj/data/3x.csv",
        mode="w",
        newline="",
        encoding="utf-8",
    ) as file:
        writer = csv.writer(file)
        writer.writerow(("date", "code", "costprice"))
        for i in range(len(data)):
            x = data[i]
            writer.writerow(
                (
                    x["date"],
                    mapping.code_to_code(x["product_code"]),
                    x["wholesale_price"],
                )
            )


if __name__ == "__main__":
    pass
    convert_xlsx_to_csv(
        "c:/Users/16028/Desktop/mcm/2023c/proj/data/附件4.xlsx",
        "c:/Users/16028/Desktop/mcm/2023c/proj/data/6x.csv",
    )
